


//
//  NRegisterViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/15.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NRegisterViewController.h"
#import "MQVerCodeImageView.h"
@interface NRegisterViewController ()

@property (weak, nonatomic) IBOutlet UIButton *regBtn;

@property (weak, nonatomic) IBOutlet UILabel *haveAccoutLabel;

@property (nonatomic, strong) MQVerCodeImageView *codeImageView;

@property (nonatomic,copy)NSString *authCode;

///验证码对应的key
@property (nonatomic,copy)NSString *authCodeKey;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topMargin;

@property (weak, nonatomic) IBOutlet UIButton *inputPwdEye;

@property (weak, nonatomic) IBOutlet UIButton *againInputPwdEye;


@end

@implementation NRegisterViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
    [self.navigationItem setItemWithTitle:@"注册" textColor:kWhiteColor fontSize:18 itemType:center];
    self.regBtn.layer.cornerRadius = kCornerRadius;
    self.regBtn.layer.masksToBounds = YES;
    //暴力点击  阻止
    self.regBtn.uxy_acceptEventInterval = 3;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(loginAction:)];
    [self.haveAccoutLabel addGestureRecognizer:tap];
    
    if (ScreenWidth>375) {
        self.topMargin.constant = 45;
    }else{
        self.topMargin.constant = 25;
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//    btn.backgroundColor = [UIColor magentaColor];
    if (ScreenWidth>375) {
        btn.frame = CGRectMake(300, 430, 70, 40);
    }else{
        btn.frame = CGRectMake(260, 360, 70, 40);
    }
    [btn setTitle:@"换一张" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(tapClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [self loaddata];
    
    //添加线条
    
    UIView *line = [UIView new];
    line.backgroundColor = kWhiteColor;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.top.equalTo(self.name.mas_bottom).offset(2);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.equalTo(@1);
    }];
    UIView *line1 = [UIView new];
    line1.backgroundColor = kWhiteColor;
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.top.equalTo(self.pwd.mas_bottom).offset(2);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.equalTo(@1);
    }];
    UIView *line2 = [UIView new];
    line2.backgroundColor = kWhiteColor;
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.top.equalTo(self.apwd.mas_bottom).offset(2);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.equalTo(@1);
    }];
    UIView *line3 = [UIView new];
    line3.backgroundColor = kWhiteColor;
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(50);
        make.top.equalTo(self.yanzhengCode.mas_bottom).offset(2);
        make.right.equalTo(self.view.mas_right).offset(-50);
        make.height.equalTo(@1);
    }];
    
    //生成本地图形验证
//    _codeImageView = [[MQVerCodeImageView alloc] initWithFrame:CGRectMake(100, 100, 80, 35)];
//    _codeImageView.bolck = ^(NSString *imageCodeStr){//看情况是否需要
//        YYLog(@"imageCodeStr = %@",imageCodeStr);
//    };
//    _codeImageView.isRotation = YES;
//    [_codeImageView freshVerCode];
//
//    //点击刷新
//    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
//    [_codeImageView addGestureRecognizer:tap1];
//    [self.view addSubview: _codeImageView];
    // Do any additional setup after loading the view from its nib.
}


///请求新的验证码
- (void)tapClick
{
    
//    [_codeImageView freshVerCode];
    //同时请求一个接口
    [self loaddata];
}
- (void)loaddata {
    
    //    [SVProgressHUD show];
    //如果不传用户ID则
//    NSDictionary *dic = @{@"messageUserId":[NUserModel shareInstance].userInfoId,@"size":@(10),@"curPage":@(1)};
    
    [[NNRCNetRequest manager] getWithUrl:kAuthCodeUrl params:nil success:^(id result) {
        YYLog(@"%@",result);
        
        ///返回一个 一个图片验证码和一个key
        
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSDictionary *dic = result[@"page"];
            UIImageView *imagev;
            if (ScreenWidth>375) {
                 imagev = [[UIImageView alloc] initWithFrame:CGRectMake(220, 430, 80, 40)];
            }else{
                 imagev = [[UIImageView alloc] initWithFrame:CGRectMake(185, 360, 80, 40)];
            }
           
            [imagev sd_setImageWithURL:dic[@"url"]];
            [self.view addSubview:imagev];
            
            self.authCodeKey = dic[@"key"];
            
        }else{
            [AFToast showText:result[@"msg"]];
        }
    } failure:^(id failure) {
        YYLog(@"%@",failure);
    }];
}




- (void)loginAction:(UITapGestureRecognizer *)tap
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 注册
///注册
- (IBAction)regAction:(id)sender {
    
//    if(self.name.text.length==0||self.pwd.text.length==0||self.apwd.text.length==0||self.yanzhengCode.text.length==0||self.authCodeKey==0) {
//        [AFToast showText:@"请完善信息"];
//        return;
//    }
    
    ///注册接口字段存在疑问
    ///userInfoType  1 企业  0 个人   userInfoIndex随机码的key 从后台接口中获取
    if (self.name.text.length<4||self.name.text.length>20) {
        [AFToast showText:@"请输入正确的用户名和密码"];
        return;
    }
    
    if (self.pwd.text.length<6||self.pwd.text.length>20) {
        [AFToast showText:@"请输入正确的用户名和密码"];
        return;
    }
    
    if (self.apwd.text.length<6||self.apwd.text.length>20) {
        [AFToast showText:@"请输入正确的用户名和密码"];
        return;
    }
    NSDictionary *dic = @{@"userInfoName":self.name.text,@"userInfoNamePwd":self.pwd.text,@"userInfoNamePwdNew":self.apwd.text,@"userInfoCode":self.yanzhengCode.text,@"userInfoType":[NUserModel shareInstance].userInfoType,@"userInfoIndex":self.authCodeKey};
    

    [SVProgressHUD show];
    

    ///postman
    [NNetworkClient postRequestWithBaseUrl:kRegisterUrl setParameters:dic success:^(id responseObject) {
        YYLog(@"%@",responseObject);
        WeakStament(ws);

        if ([responseObject[@"code"] integerValue]==0) {
            [AFToast showText:responseObject[@"msg"]];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [ws.navigationController popViewControllerAnimated:YES];
                [SVProgressHUD dismiss];
            });
        }else {
            [AFToast showText:responseObject[@"msg"]];
            [SVProgressHUD dismiss];
        }
    } fail:^(NSError *error) {

        YYLog(@"%@",error);

    } delegater:nil];

    
//    [[NNRCNetRequest manager]postWithUrl:kRegisterUrl params:dic success:^(id result) {
//
//        WeakStament(ws);
//        YYLog(@"%@",result);
//        if ([result[@"code"] integerValue]==0) {
//            [AFToast showText:result[@"msg"]];
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [ws.navigationController popViewControllerAnimated:YES];
//                [SVProgressHUD dismiss];
//            });
//        }else {
//            [AFToast showText:result[@"msg"]];
//            [SVProgressHUD dismiss];
//        }
//
//    } failure:^(id failure) {
//        YYLog(@"%@",failure);
//    }];
    
}

///眼睛点击睁开
- (IBAction)eyeClicked:(UIButton *)sender {
    
    //按钮反选
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender setImage:[UIImage imageNamed:@"openeye"] forState:UIControlStateSelected];
        if (sender.tag==111) {
            self.pwd.secureTextEntry = NO;
        }else{
            self.apwd.secureTextEntry = NO;
        }
        
        
    }else{
        [sender setImage:[UIImage imageNamed:@"closeye"] forState:UIControlStateNormal];
        if (sender.tag==111) {
            self.pwd.secureTextEntry = YES;
        }else{
            self.apwd.secureTextEntry = YES;
        }
    }
}




- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
